package com.corpgovernment.organization.controller;

import com.corpgovernment.api.organization.model.org.OrgConfigInfoVo;
import com.corpgovernment.api.organization.model.org.request.GetOrgConfigBatchRequest;
import com.corpgovernment.api.organization.model.org.request.GetOrgConfigRequest;
import com.corpgovernment.api.organization.model.org.request.SaveOrgConfigRequest;
import com.corpgovernment.api.organization.model.org.response.GetOrgConfigBatchResponse;
import com.corpgovernment.api.organization.model.org.response.GetOrgConfigResponse;
import com.corpgovernment.common.base.CommonException;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.organization.constant.CommonConst;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.corpgovernment.organization.service.IOrgConfigService;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * 组织设置信息控制器
 *
 * @author dlz
 * @since 2023/5/6
 */
@RestController
@RequestMapping("/orgConfig")
@Slf4j
public class OrgConfigController {
    @Autowired
    private IOrgConfigService orgConfigService;

    private final Set<String> tfSet = Sets.newHashSet(CommonConst.TRUE, CommonConst.FALSE);

    @RequestMapping("/save")
    public JSONResult<Void> save(@RequestBody SaveOrgConfigRequest request) {
        OrgConfigInfoVo orgConfigInfo = request.getOrgConfigInfo();
        if (!tfSet.contains(orgConfigInfo.getProjectIsRequired())) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.PROJECT_IS_REQUIRED_ERROR);
        }
        if (!tfSet.contains(orgConfigInfo.getCostCenterIsRequired())) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.COST_CENTER_IS_REQUIRED_ERROR);
        }
        orgConfigService.saveOrgConfig(request);
        return JSONResult.ok();
    }

    @RequestMapping("/get")
    public JSONResult<GetOrgConfigResponse> get(@RequestBody GetOrgConfigRequest request) {
        return JSONResult.success(orgConfigService.getOrgConfig(request));
    }

    @RequestMapping("/getOrgConfigBatch")
    public JSONResult<GetOrgConfigBatchResponse> getOrgConfigBatch(@RequestBody GetOrgConfigBatchRequest request){
        return JSONResult.success(orgConfigService.getOrgConfigBatch(request));
    }
}
